package com.sdp.khy.baotou.task;

import com.sdp.khy.baotou.db.khy_user_coupon.entity.KhyUserCoupon;
import com.sdp.khy.baotou.db.khy_user_coupon.service.IKhyUserCouponService;
import com.sdp.right.cron.ScheduledOfTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Component
public class KhyUserCouponTack implements ScheduledOfTask {

    @Autowired
    private IKhyUserCouponService iKhyUserCouponService;

    /**
     * 每天凌晨查询优惠卷，修改过期优惠卷状态
     */
    @Override
    public void execute() {
                List<KhyUserCoupon> khyUserCouponList=iKhyUserCouponService.list();
                List<KhyUserCoupon> updateKhyUserCoupons=new ArrayList<>();
                LocalDate  newDate=LocalDate.now();
                if(khyUserCouponList.size()>0){
                    for(KhyUserCoupon khyUserCoupon:khyUserCouponList){
                        if(khyUserCoupon.getEndDate().isBefore(newDate)){
                            khyUserCoupon.setCouponStatus("3");
                            updateKhyUserCoupons.add(khyUserCoupon);
                        }
                    }
                    iKhyUserCouponService.updateBatchById(updateKhyUserCoupons);
                }

    }
}
